import time
import os
import pytest
from utils.datas import TestDatas


def pytest_configure(config):
    """
    设置日志文件路径
    :param config:
    :return:
    """
    time_now = time.strftime("%Y-%m-%d_%H-%M-%S")
    config.option.log_file = os.path.join(config.rootdir, 'logs', f'{time_now}.log')


def pytest_collection_modifyitems(items):
    """
    测试用例收集完成时，将收集到的item的name和nodeid的中文显示
    :return:
    """
    for item in items:
        item.name = item.name.encode("utf-8").decode("unicode_escape")
        item._nodeid = item.nodeid.encode("utf-8").decode("unicode_escape")


def generate_test_datas():
    datas = []
    for i in range(3):
        datas.append((TestDatas.generate_name(), TestDatas.generate_phone_number()))
    TestDatas.save_to_yaml(f"{os.getcwd()}/test_datas/data.yaml", datas)
    return datas


def get_test_datas():
    return TestDatas.read_yaml_datas(f"{os.getcwd()}/test_datas/data.yaml")


@pytest.fixture(params=generate_test_datas())
def add_member_datas(request):
    return request.param


@pytest.fixture(params=get_test_datas())
def delete_datas(request):
    return request.param[0]